Omplus.base.define(Omplus.models.Model, {
    omplusName:"GroupsSearch",
    controller:"userController",
    value:{
        "user_id":{
            omplusType:"Number"
        },
        "name":{
            omplusType:"String"
        },
        "last_name":{
            omplusType:"String"
        },
        "nick_name":{
            omplusType:"String"
        },
        "username":{
            omplusType:"String"
        }
    }
});

Omplus.models.GroupsSearchModelProto = Omplus.base.extend(Omplus.models.Object, {
    omplusType:"GroupsSearchModelProto",
    value:{
        backoffice_group_id:{
            omplusType:"Number"
        },
        backoffice_group_name:{
            omplusType:"String"
        }
    }
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"GroupsSearchResults",
    controller:"userController",
    value:{
        backoffice_groups:{
            omplusType:"Array",
            omplusProto:"GroupsSearchModelProto"
        }
    }
});

Omplus.models.GroupsProto = Omplus.base.extend(Omplus.models.Model, {
    omplusType:"GroupsProto",
    value:{
        "backoffice_group_id":{
            omplusType:"Number"
        },
        "backoffice_group_name":{
            omplusType:"String"
        },
        functions:{
            omplusType:"String"
        }
    }
});

Omplus.base.define(Omplus.models.SingleChooser, {
    omplusName:"groupsChooser",
    controller:"userController",
    omplusProto:"GroupsProto"
});

Omplus.views.OpeningGroupsTabView = Omplus.base.extend(Omplus.views.OpeningTabView, {
    omplusType:"OpeningGroupsTabView",
    layout:{
        omplusType:"AccordionLayout"
    },
    controller:"userController",
    components:[
        {
            omplusName:"groupDetailsFirstPanel",
            omplusType:"AccordionPanel",
            components:[
                {
                    model:"groupsChooser.current.backoffice_group_id",
                    labelModel:"Assets.list.groups_id_label.localValue",
                    inputType:"Label"
                },
                {
                    model:"groupsChooser.current.backoffice_group_name",
                    labelModel:"Assets.list.name_label.localValue",
                    inputType:"Label"
                },
                {
                    model:"groupsChooser.current.functions",
                    labelModel:"Assets.list.functions_label.localValue",
                    inputType:"List",
                    showModel:"name",
                    listModel:"list",
                    height:"auto !important",
                    errorable:false
                }
            ],
            controller:"languageController",
            model:"Assets.list.group_details_label.localValue"
        }
    ]
});

Omplus.base.define(Omplus.views.ManagementPanel, {
    omplusName:"groupsPanel",
    controller:"userController",
    renderTo:"managements",
    models:{
        chooser:"groupsChooser",
        search:"GroupsSearch",
        searchResult:"GroupsSearchResults",
        searchResultList:"backoffice_groups",
        forDisplay:"backoffice_group_name"
    },
    views:{
        chooser:{
            title:"Assets.list.groups_label.localValue",
            components:[
                {
                    omplusName:"groupsTabs",
                    omplusType:"OpeningTabPanel",
                    omplusProto:"OpeningGroupsTabView",
                    controller:"userController",
                    model:"groupsChooser",
                    showModel:"backoffice_group_name"
                }
            ]
        },
        search:[
            {
                // Per page
                inputType:"Select",
                model:"PerPages",
                showModel:"number",
                current:"forGroups",
                controller:"languageController",
                labelModel:"Assets.list.label_per_page.localValue",
                perPage:true
            },
            {
                // Search button
                inputType:"Button",
                controller:"languageController",
                model:"Assets.list.label_search_button.localValue",
                cssClasses:["search-button"],
                labelWidth:"0%",
                inputWidth:"100%",
                searchButton:true
            }
        ],
        searchResult:[
            {
                labelModel:"Assets.list.groups_id_label.localValue",
                width:"80px",
                omplusType:"Label",
                model:"backoffice_group_id"
            },
            {
                labelModel:"Assets.list.name_label.localValue",
                width:"150px",
                omplusType:"Label",
                model:"backoffice_group_name"
            },
            {
                omplusType:"Button",
                model:"Assets.list.details_label.localValue",
                width:"250px",
                controller:"languageController",
                getDetails:true,
                errorable:false
            },
            {
                omplusType:"Button",
                model:"Assets.list.label_delete.localValue",
                width:"200px",
                controller:"languageController",
                click:function () {
                    var group = $(this).closest(".tableRow").find("label:first").text();
                    Omplus.globals.getObject("NewGroup.for_deletion").setValue(group);
                    Omplus.globals.getObject("DeleteGroup").show();
                }
            }
        ]
    },
    components:[
        {
            omplusType:"TabPanel",
            omplusName:"addGroupPanel",
            layout:{
                omplusType:"HorizontalLayout"
            },
            controller:"languageController",
            model:"Assets.list.new_label.localValue",
            components:[
                {
                    omplusType:"Panel",
                    omplusName:"addGroupLeftPanel",
                    layout:{
                        omplusType:"VerticalLayout"
                    },
                    width:"370px",
                    components:[
                        {

                            omplusType:"Button",
                            omplusName:"getFunctionsForChoosing",
                            model:"Assets.list.get_functions_label.localValue",
                            controller:"languageController",
                            click:function () {
                                console.log();
                                Omplus.globals.getObject("NewGroup").setValue({
                                        "available_functionalities":Omplus.globals.getObject("User.privileges.current").getValue()["functionalities"]
                                    }
                                );
                                $(this).parent().find(".button").replaceWith("");
                            },
                            width:"235px"
                        },
                        {
                            omplusType:"Panel",
                            omplusName:"availableFunctionsPanel",
                            layout:{
                                omplusType:"VerticalLayout",
                                width:"170px"
                            },
                            components:[
                                {
                                    omplusType:"Label",
                                    omplusName:"privilegeFunctionListLabel",
                                    model:"Assets.list.available_functions_label.localValue",
                                    controller:"languageController"
                                },
                                {
                                    omplusType:"List",
                                    omplusName:"privilegeFunctionList",
                                    controller:"userController",
                                    model:"NewGroup",
                                    showModel:"functionality",
                                    listModel:"available_functionalities",
                                    click:function (item) {
                                        var index = item.attr("data-index");
                                        var array = Omplus.globals.getObject("NewGroup.available_functionalities").getValue();
                                        var taken = array[index];
                                        delete array[index];
                                        var used_array = Omplus.globals.getObject("NewGroup").getValue()["functionalities"];
                                        used_array.push(taken);
                                        Omplus.globals.getObject("NewGroup").setValue({
                                            functionalities:used_array
                                        });
                                        Omplus.globals.getObject("NewGroup").setValue({
                                            available_functionalities:array
                                        });
                                    },
                                    errorable:false,
                                    width:"170px",
                                    cssClasses:[
                                        "privilegeFunctionList"
                                    ]
                                }
                            ],
                            width:"170px"
                        },
                        {
                            omplusType:"Panel",
                            omplusName:"newFunctionsPanel",
                            layout:{
                                omplusType:"VerticalLayout",
                                width:"170px"
                            },
                            components:[
                                {
                                    omplusType:"Label",
                                    omplusName:"newPrivilegeFunctionListLabel",
                                    model:"Assets.list.new_functions_label.localValue",
                                    controller:"languageController"
                                },
                                {
                                    omplusType:"List",
                                    omplusName:"newPrivilegeFunctionList",
                                    controller:"userController",
                                    model:"NewGroup",
                                    showModel:"functionality",
                                    listModel:"functionalities",
                                    click:function (item) {
                                        var index = item.attr("data-index");
                                        var array = Omplus.globals.getObject("NewGroup.functionalities").getValue();
                                        var taken = array[index];
                                        delete array[index];
                                        var used_array = Omplus.globals.getObject("NewGroup").getValue()["available_functionalities"];
                                        used_array.push(taken);
                                        Omplus.globals.getObject("NewGroup").setValue({
                                            available_functionalities:used_array
                                        });
                                        Omplus.globals.getObject("NewGroup").setValue({
                                            functionalities:array
                                        });
                                    },
                                    errorable:false,
                                    width:"170px",
                                    cssClasses:[
                                        "privilegeFunctionList"
                                    ]
                                }
                            ],
                            width:"170px"
                        }
                    ]
                },
                {
                    omplusType:"Panel",
                    omplusName:"addGroupMiddlePanel",
                    layout:{
                        omplusType:"VerticalLayout"
                    },
                    width:"300px",
                    components:[
                        {
                            omplusType:"LabelInputPair",
                            omplusName:"addGroupName",
                            labelModel:"Assets.list.name_label.localValue",
                            labelController:"languageController",
                            model:"NewGroup.name",
                            controller:"userController"
                        }
                    ]
                },
                {
                    omplusType:"Panel",
                    omplusName:"addGroupRightPanel",
                    layout:{
                        omplusType:"VerticalLayout"
                    },
                    width:"300px",
                    components:[
                        {
                            omplusType:"Button",
                            omplusName:"addGroup",
                            model:"Assets.list.add_label.localValue",
                            controller:"languageController",
                            errorable:true,
                            click:function () {
                                Omplus.globals.getObject("NewGroup").getAction("addGroup").run();
                            }
                        },
                        {
                            omplusType:"Label",
                            omplusName:"addUserCreated",
                            model:"Assets.list.user_created_label.localValue",
                            controller:"languageController"
                        }
                    ]
                },
                {
                    omplusType:"Panel",
                    omplusName:"addUserOverlayPanel",
                    renderTo:"addGroupPanel",
                    width:"900px",
                    showEffect:{
                        name:"fade",
                        time:1
                    },
                    hideEffect:{
                        name:"fade",
                        time:1
                    }
                }
            ]
        }
    ]
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"NewGroup",
    value:{
        name:{
            omplusType:"String",
            errorable:true,
            nullable:false,
            validators:["EmptyValidator"]
        },
        functionalities:{
            omplusType:"Array",
            omplusProto:"FunctionalitiesProto"
        },
        available_functionalities:{
            omplusType:"Array",
            omplusProto:"FunctionalitiesProto"
        },
        for_deletion:{
            omplusType:"String"
        }
    },
    actions:[
        {
            omplusType:"Get",
            omplusName:"deleteGroup",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"NewGroup",
            responseModel:"GroupsSearchResults"
        },
        {
            omplusType:"Get",
            omplusName:"addGroup",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"NewGroup"
        }
    ],
    controller:"userController"
});

Omplus.models.FunctionalitiesProto = Omplus.base.extend(Omplus.models.Object, {
    omplusType:"FunctionalitiesProto",
    value:{
        functionality:{
            omplusType:"String"
        }
    }
});

Omplus.base.define(Omplus.views.Dialog, {
    omplusName:"DeleteGroup",
    width:"500px",
    height:"100px",
    components:[
        {
            omplusType:"Label",
            omplusName:"deleteGroupQuestion",
            model:"Assets.list.delete_group_question_label.localValue",
            controller:"languageController",
            width:"100%"
        }
    ],
    confirmClick:function () {
        Omplus.globals.getObject("NewGroup").getAction("deleteGroup").run();
        Omplus.globals.getObject("DeleteGroup").hide();
    }
});